//
//  HDPreCollectionView.swift
//  HuaDaoPeifu
//
//  Created by Apple on 2018/6/19.
//  Copyright © 2018年 paiyipai. All rights reserved.
//

import Foundation

@objc protocol HDPreCollectionViewDelegate{
    
    func preCollectionViewHeightChange(height:CGFloat)
}

class HDPreCollectionView: UIView {
    var uploadCollectionView:UICollectionView?
    var delegate:HDPreCollectionViewDelegate?
    var dataArray = [NSDictionary]()
    var totalHeight:CGFloat = 0
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        let flowLayout = UICollectionViewFlowLayout();
        uploadCollectionView = UICollectionView(frame: CGRect(x: 0, y:0, width: AppConstant.screenWidth, height:750), collectionViewLayout: flowLayout)
        uploadCollectionView?.backgroundColor = UIColor.white
        self.addSubview(uploadCollectionView!)
        uploadCollectionView?.delegate = self
        uploadCollectionView?.dataSource = self
        uploadCollectionView?.isScrollEnabled = false
        uploadCollectionView?.register(HDPayUploadCell.self, forCellWithReuseIdentifier: "collectionCell");
        uploadCollectionView?.register(HDCollectionHeaderView.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: "headerView")
        
        
    }
    func commentSetting(){
        self.getData()
    }
    
    func getData() {
        
        let dataDict1 = ["title" : "申请赔付", "pics" : ["1","1","2","3","4","5","6"]]as NSDictionary
        let dataDict2 = ["title" : "申请赔付", "pics" : ["1","2","3"]] as NSDictionary
        let dataDict3 = ["title" : "申请赔付", "pics" : ["1","2","3","4","5","6","7","8","9"]] as NSDictionary
        dataArray =  [dataDict1,dataDict2,dataDict3]
        self.caculateHeightWithArray(array: dataArray as [NSDictionary])
    }
    
    func caculateHeightWithArray(array:[NSDictionary]){
        totalHeight = 30
        for dict in array {
            var height:CGFloat = 0
            if let picsArr =  dict.object(forKey:"pics") as? [String]{
                if picsArr.count <= 4{
                    height = 30 + (AppConstant.screenWidth/4.0 - 50 / 4.0) + 20
                }else if (picsArr.count <= 8 && picsArr.count >= 4){
                    height = 30  + (AppConstant.screenWidth/4.0 - 50 / 4.0) * 2 + 30
                }else{
                    height = 30  +  3 * (AppConstant.screenWidth/4.0 - 50 / 4.0) + 40
                }
                totalHeight = totalHeight + height
            }
        }
        uploadCollectionView?.frame = CGRect(x: 0, y: 50, width: AppConstant.screenWidth, height:totalHeight - 50)
        //代理
        self.delegate?.preCollectionViewHeightChange(height: totalHeight)
    }
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}


extension HDPreCollectionView:UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout{
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 3
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        let dic = dataArray[section]
        let picsArr = dic.object(forKey: "pics") as? NSArray
        return picsArr!.count
        
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "collectionCell", for: indexPath) as! HDPayUploadCell
        cell.indexP = indexPath
//        let dic = dataArray[indexPath.section]
//        let picsArr = dic.object(forKey: "pics") as? NSArray
//            cell.isEditble = false
            cell.imageV.image = nil
        
        return cell
    }
    
    //每个分区的内边距
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return UIEdgeInsetsMake(10, 10, 10, 10);
    }
    
    //最小 item 间距
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 10;
    }
    
    //最小行间距
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 10;
    }
    
    //item 的尺寸
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: AppConstant.screenWidth/4.0 - 50 / 4.0, height: AppConstant.screenWidth/4.0 - 50 / 4.0)
    }
    
    //每个分区区头尺寸
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
        return CGSize(width: AppConstant.screenWidth, height: 30);
    }
    
    func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        var reusableView :UICollectionReusableView?
        let header :HDCollectionHeaderView = collectionView.dequeueReusableSupplementaryView(ofKind:UICollectionElementKindSectionHeader, withReuseIdentifier: "headerView", for: indexPath) as! HDCollectionHeaderView
        //        header.backgroundColor = UIColor.orange
        reusableView = header
        return reusableView!
    }
    
}

